//
//  HJLocationVC.m
//  project
//
//  Created by jc on 2019/5/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJLocationVC.h"
#import "HJMineVC.h"
#import "YFLocationManager.h"
#import "HJCityListVC.h"
#import "HJTabBarViewController.h"
#import "HJCityManager.h"

@interface HJLocationVC ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UIButton *selecteBtn;

@property (weak, nonatomic) IBOutlet UITextView *addressDetailView;
@property (weak, nonatomic) IBOutlet UILabel *placeHolder;
@property (weak, nonatomic) IBOutlet UIButton *goBtn;

@property (weak, nonatomic) NSString *cityOid;
@end

@implementation HJLocationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navTitle = @"更改地址";
    
    @weakify(self)
    [YFLocationManager.shared onceLocating:^(CLLocation * _Nullable location, AMapLocationReGeocode * _Nullable regeocode, NSString * _Nullable error) {
        @strongify(self)
        self.cityLabel.text = regeocode.city;
        self.cityOid = [NSString stringWithFormat:@"%zd",[[HJCityManager manager] cityIdWithFormCity:regeocode.city]];
    }];
    
    [self.view setupShadowWithView:self.bgView];
    [self.view setupShadowWithView:self.addressDetailView];
    [self.view setupShadowWithView:self.goBtn];
    [mNotificationCenter addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    
    [[self.selecteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        HJCityListVC * listVC =[HJCityListVC new];
        listVC.cityBlock = ^(NSDictionary * _Nonnull cityDic) {
            LKLog(@"%@",cityDic);
            self.cityLabel.text = cityDic[@"cityName"];
            self.cityOid = cityDic[@"cityOid"];
        };
        [self.navigationController pushViewController:listVC animated:YES];
    }];
    
    RAC(self.goBtn,userInteractionEnabled) = [RACSignal combineLatest:@[self.addressDetailView.rac_textSignal] reduce:^id (NSString * content){
        @strongify(self)
        if (!content.length) {
            [self.goBtn setBackgroundColor:UIColor.lightGrayColor];
            return @NO;
        }
        [self.goBtn setBackgroundColor:UIColor.blackColor];
        return @YES;
    }];
    [[self.goBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if (self.cityOid==0) return;
        [HJUserManager updateUserModelWithParams:@{@"address":self.addressDetailView.text,@"cityOid":self.cityOid} success:^{
             [self.navigationController popViewControllerAnimated:YES];
//            HJTabBarViewController * tabbar = (HJTabBarViewController *)self.tabBarController;
//            [tabbar setSelectedIndex:0];
//            [self.navigationController popToRootViewControllerAnimated:YES];
//            for (UIViewController * VC in self.navigationController.viewControllers) {
//                if ([VC isKindOfClass:[HJMineVC class]]) {
//                    [self.navigationController popToViewController:(HJMineVC *)VC animated:YES];
//                }
//            }
        }];
    }];
    
}

-(void)textViewDidChange:(NSNotification *)textView {
    if (self.addressDetailView.text.length) {
        _placeHolder.hidden = YES;
    }else{
        _placeHolder.hidden = NO;
    }
}

@end
